package io.renren.service.impl;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import io.renren.constant.AppConstant;
import io.renren.dao.ADDao;
import io.renren.po.AD;
import io.renren.vo.ADExample;
import io.renren.vo.RespModel;
import io.renren.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("adService")
public class AdServiceImpl implements AdService {

	@Autowired
	private ADDao adMapper;

	public void insert(AD ad) {
		adMapper.insertSelective(ad);
	}

	public void delete(AD ad) {
		ADExample example = new ADExample();
		ADExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(ad.getId());
		adMapper.deleteByExample(example);
	}

	public void update(AD ad) {
		ADExample example = new ADExample();
		ADExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(ad.getId());
		adMapper.updateByExampleSelective(ad, example);
	}

	@Override
	public AD select(AD ad) {
		ADExample example = new ADExample();
		ADExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(ad.getId());
		List<AD>list = adMapper.selectByExample(example);
		if(list.size()>0)
		{
			AD tempAD = list.get(0);
			return tempAD;
		}
		return null;
	}

	public RespModel<List<AD>> selectAll(AD adPage) {
		ADExample example = new ADExample();
		ADExample.Criteria criteria = example.createCriteria();
		String type = adPage.getType();
		String name = adPage.getName();
		if(org.apache.commons.lang.StringUtils.isNotBlank(type) && type.length()>0) {
			criteria.andTypeEqualTo(type);
		}
		if(StringUtils.isNotEmpty(name)) {
			String tempname = "%" +name + "%";
			criteria.andNameLike(tempname);
		}
		example.setStart(adPage.getStart());
		example.setLimit(adPage.getLimit());
		example.setOrderByClause("id desc");
		
		List<AD> listTemp= adMapper.selectByExample(example);
		int count = adMapper.countByExample(example);
		
		return new RespModel(AppConstant.STATU_SUCCESS.getName(),AppConstant.STATU_SUCCESS.getValue(),String.valueOf(count),listTemp);
	}

	/**
	 * 获取开机广告
	 * @return
	 */
	public RespModel<List<Map<String,Object>>> getOpenAdvertList(){
		List<Map<String,Object>> list = adMapper.getOpenAdvertList();
		return new RespModel<>(AppConstant.STATU_SUCCESS.getName(),AppConstant.STATU_SUCCESS.getValue(),"",list);
	}

	
}
